package com.ny.zmb.openapi.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ny.zmb.glxt.entity.PmStore;
import com.ny.zmb.glxt.mapper.PmStoreMapper;
import com.ny.zmb.openapi.entity.PmRegister;
import com.ny.zmb.openapi.enums.TransactionStatusEnum;
import com.ny.zmb.openapi.mapper.PmRegisterMapper;
import com.ny.zmb.openapi.mapper.merchant.MerchantWithdrawMapper;
import com.ny.zmb.openapi.service.IPmMerchantService;
import com.ny.zmb.openapi.ysutil.YsHttpUtil;
import com.ny.zmb.openapi.ysutil.common.ServiceEnum;
import com.ny.zmb.openapi.ysutil.common.YsPayConstant;
import com.ny.zmb.openapi.ysutil.from.merchant.*;
import com.ny.zmb.openapi.ysutil.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.ny.zmb.openapi.constant.PHPConstant.JAVA_BASE_URL;

@Service
public class PmMerchantServiceImpl implements IPmMerchantService {

    @Resource
    private YsHttpUtil ysHttpUtil;
    @Resource
    private PmStoreMapper pmStoreMapper;
    @Resource
    private PmRegisterMapper pmRegisterMapper;
    @Resource
    private MerchantWithdrawMapper merchantWithdrawMapper;

    QueryWrapper<PmStore> queryWrapperPmStore = new QueryWrapper<>();
    QueryWrapper<PmRegister> queryWrapperPmRegister = new QueryWrapper<>();
    QueryWrapper<MerchantWithdraw> queryWrapperMerchantWithdraw = new QueryWrapper<>();


    @Override
    public JSONObject addBankAccount(MerchantAddBankAccount obj) {
        JSONObject jsonObject = new JSONObject();
        ServiceEnum anyEnum = ServiceEnum.CONTRACT_QUERY;
        jsonObject = ysHttpUtil.sendPlus(anyEnum, obj);
        return jsonObject;
    }

    @Override
    public JSONObject addBankAccount(MerchantAddBankAccount obj, Integer userId) {
        JSONObject jsonObject = new JSONObject();
        ServiceEnum anyEnum = ServiceEnum.CONTRACT_QUERY;
        jsonObject = ysHttpUtil.sendPlus(anyEnum, obj);
        return jsonObject;
    }

    @Override
    public JSONObject bankAccountQuery(MerchantBankAccountQuery obj) {
        JSONObject jsonObject = new JSONObject();
        ServiceEnum anyEnum = ServiceEnum.BANK_ACCOUNT_QUERY;
        jsonObject = ysHttpUtil.sendPlus(anyEnum, obj);
        return jsonObject;
    }

    @Override
    public JSONObject bankAccountQuery(MerchantBankAccountQuery obj, Integer userId) {
        JSONObject jsonObject = new JSONObject();
        ServiceEnum anyEnum = ServiceEnum.BANK_ACCOUNT_QUERY;
        jsonObject = ysHttpUtil.sendPlus(anyEnum, obj);
        return jsonObject;
    }

    @Override
    public JSONObject changePayment(MerchantChangePayment obj) {
        JSONObject jsonObject = new JSONObject();
        ServiceEnum anyEnum = ServiceEnum.CHANGE_PAYMENT;
        jsonObject = ysHttpUtil.sendPlus(anyEnum, obj);
        return jsonObject;
    }

    @Override
    public JSONObject changePayment(MerchantChangePayment obj, Integer userId) {
        JSONObject jsonObject = new JSONObject();
        ServiceEnum anyEnum = ServiceEnum.CHANGE_PAYMENT;
        jsonObject = ysHttpUtil.sendPlus(anyEnum, obj);
        return jsonObject;
    }

    @Override
    public JSONObject merchantWithdraw(MerchantWithdraw obj) {
        JSONObject jsonObject = new JSONObject();
        ServiceEnum anyEnum = ServiceEnum.MERCHANT_WITHDRAW;
        jsonObject = ysHttpUtil.sendPlus(anyEnum, obj);
        return jsonObject;
    }

    @Override
    public JSONObject merchantWithdraw(MerchantWithdraw obj, Integer userId) {
        JSONObject jsonObject = new JSONObject();
        ServiceEnum anyEnum = ServiceEnum.MERCHANT_WITHDRAW;
        if (obj.getMerchantNo() == null || "".equals(obj.getMerchantNo())) {
            queryWrapperPmRegister = new QueryWrapper<>();
            queryWrapperPmRegister.eq("user_id", userId);
            PmRegister pmRegister = pmRegisterMapper.selectOne(queryWrapperPmRegister);
            obj.setMerchantNo(pmRegister.getMerchantNo());
        }
        if (obj.getBankAccountNo() == null || "".equals(obj.getBankAccountNo())) {
            queryWrapperPmRegister = new QueryWrapper<>();
            queryWrapperPmRegister.eq("user_id", userId);
            PmRegister pmRegister = pmRegisterMapper.selectOne(queryWrapperPmRegister);
            obj.setBankAccountNo(pmRegister.getAccountCardno());
        }
        // 创建
        obj.create(userId);
        if (StringUtils.isEmpty(obj.getRemark())) {
            obj.setRemark(obj.getMerchantNo() + "提现");
        }
        if (StringUtils.isEmpty(obj.getAccountType())) {
            obj.setAccountType("01");
        }
        // 设置状态为 正在处理
        obj.setStatus(0);
        obj.setUserId(userId);
        obj.setNotifyUrl(JAVA_BASE_URL + "/notify" + "/test/withdrawNotify");
        merchantWithdrawMapper.insert(obj);
        jsonObject = ysHttpUtil.sendPlus(anyEnum, obj);
        // 状态赋值
        obj.setStatus(obj.getTransactionStatus(jsonObject.getString("state")));
        obj.update(jsonObject, userId);
        // 更新
        merchantWithdrawMapper.updateById(obj);
        return jsonObject;
    }

    @Override
    public JSONObject queryWithdraw(String requestNo) {
        JSONObject jsonObject = new JSONObject();
        ServiceEnum anyEnum = ServiceEnum.QUERY_WITHDRAW;
        jsonObject.put("requestNo", requestNo);
        return ysHttpUtil.sendPlus(anyEnum, jsonObject);
    }

    @Override
    public JSONObject queryWithdraw(String requestNo, Integer userId) {
        JSONObject result = new JSONObject();
        JSONObject request = new JSONObject();
        queryWrapperMerchantWithdraw = new QueryWrapper<>();
        queryWrapperMerchantWithdraw.eq("request_no", requestNo);
        MerchantWithdraw merchantWithdraw = merchantWithdrawMapper.selectOne(queryWrapperMerchantWithdraw);
        // 没有找到记录
        if (merchantWithdraw == null) {
            return null;
        }
        ServiceEnum anyEnum = ServiceEnum.QUERY_WITHDRAW;
        request.put("requestNo", requestNo);
        result = ysHttpUtil.sendPlus(anyEnum, request);
        // 状态赋值
        merchantWithdraw.setStatus(merchantWithdraw.getTransactionStatus(result.getString("state")));
        merchantWithdraw.update(result, userId);
        // 更新
        merchantWithdrawMapper.updateById(merchantWithdraw);
        return result;
    }

    @Override
    public JSONObject queryBalances(JSONObject obj) {
        JSONObject result = ysHttpUtil.sendPlus(ServiceEnum.QUERY_AMOUNT, obj);
        return result;
    }

    @Override
    public IPage<MerchantWithdraw> page(IPage<MerchantWithdraw> page, QueryWrapper<MerchantWithdraw> queryWrapper) {
        return merchantWithdrawMapper.selectPage(page, queryWrapper);
    }

    public MerchantWithdraw getByRequestNo(String requestNo) {
        queryWrapperMerchantWithdraw = new QueryWrapper<>();
        queryWrapperMerchantWithdraw.eq("request_no", requestNo);
        return merchantWithdrawMapper.selectOne(queryWrapperMerchantWithdraw);
    }

    public boolean updateById(MerchantWithdraw merchantWithdraw) {
        return merchantWithdrawMapper.updateById(merchantWithdraw) == 1;
    }
}
